<?php


/**
 * 内容管理操作类 contentHandler
 * @package  htdocs
 * @subpackage  module/base
 * @version 1.0
 */

class contentHandler {

    private $dbca;//数据库实例

    function __construct(&$dbca) {
        $this->dbca = $dbca;
    }

    function __destruct() {

    }

    /**
     * 查询商品咨询信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'pcustID' => int 客户号
     *       'consultStatus' => int 0:未回复 1:已回复
     *       'recommendFlg' => int 首页推荐标志 0:不推荐 1:推荐
     *       'displayFlg' => int 显示标志 0:针对此用户显示 1:对所有用户显示
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *      'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null|array
     */
    public function searchConsult( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //二级网购号
        if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
            $sql .= ' AND cc.twoPostID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['twoPostID'];
        }

        //回复状态
        if (isset($arg_array['consultStatus']) && is_numeric($arg_array['consultStatus'])) {
            $sql .= ' AND cc.consultStatus = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['consultStatus'];
        }

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND cc.pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //首页推荐标志 0:不推荐 1:推荐
        if (isset($arg_array['recommendFlg']) && in_array($arg_array['recommendFlg'], array(0 ,1))) {
            $sql .= ' AND cc.recommendFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['recommendFlg'];
        }

        //显示标志 0:针对此用户显示 1:对所有用户显示
        if (isset($arg_array['displayFlg']) && in_array($arg_array['displayFlg'], array(0, 1))) {
            $sql .= ' AND cc.displayFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['displayFlg'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_consult cc ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT cc.*,cm.custID,cm.head_pic,cm.rankName FROM content_consult cc
                           INNER JOIN cust_master cm ON cm.pcustID = cc.pcustID ' . $sql;
            error_log($search_sql);
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'custID' => $item->custID,
                    'head_pic' => set_cust_head_pic( $item->head_pic ),
                    'rankName' => $item->rankName,
                    'goodsName' => $item->goodsName,
                    'link' => set_goods_link( $item->postID ),
                    'img_link' => set_goods_img_link( $item->goodsMainPic ),
                    'createTime' => $item->createTime,
                    'info' => $item->consultInfo,
                    'ip' => $item->ipAddress,
                    'status' => $item->commentStatus,
                    'answerInfo' => $item->answerInfo,
                    'answerUserID' => $item->answerUserID,
                    'answerDateTime' => $item->answerDateTime,
                    'satisfaction' => $item->satisfaction,
                    'creator' => $item->creator,
                    'consultStatus' => $item->consultStatus
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 咨询回复评价
     * @param array $arg_array 参数数组
     * @return array
     * @throws Exception
     */
    public function changeConsultReplySatisfied( $arg_array ) {

        if (!isset($arg_array['uuid'], $arg_array['satisfaction']) || empty($arg_array['uuid']) || !in_array($arg_array['satisfaction'], array(1, 2)))
            return array('code' => 400, 'message' => '参数错误');

        try {

            $consult_obj = new entity('content_consult');
            $consult_obj->uuid = $arg_array['uuid'];
            if (!$this->dbca->fetch($consult_obj))
                throw new Exception("咨询信息没有找到", 404);
            if ($consult_obj->satisfaction == 1 || $consult_obj->satisfaction == 2)
                throw new Exception("咨询已评价", 406);
            $consult_obj->satisfaction = $arg_array['satisfaction'];
            if (!$this->dbca->save($consult_obj))
                throw new Exception("页面已失效，请刷新页面重试", 404);

            return array( 'code' => 200, 'message' => 'ok' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }


    }

    /**
     * 查询商品评价总数
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
            'pcustID' => int 客户号
            'recommendFlg' => int 首页推荐标志 0:不推荐 1:推荐
            'displayFlg' => int 显示标志 0:针对此用户显示 1:对所有用户显示
     * )
     * @return null
     */
    public function searchCommentsCount($arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND cc.pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //UUID
        if (isset($arg_array['uuid']) && is_string($arg_array['uuid'])) {
            $sql .= ' AND cc.uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //首页推荐标志 0:不推荐 1:推荐
        if (isset($arg_array['recommendFlg']) && in_array($arg_array['recommendFlg'], array(0 ,1))) {
            $sql .= ' AND cc.recommendFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['recommendFlg'];
        }

        //显示标志 0:针对此用户显示 1:对所有用户显示
        if (isset($arg_array['displayFlg']) && in_array($arg_array['displayFlg'], array(0, 1))) {
            $sql .= ' AND cc.displayFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['displayFlg'];
        }

        //二级商品号
        if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
            $sql .= ' AND cc.twoPostID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['twoPostID'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try{

            //统计分类数量
            $search_sql = 'SELECT COUNT(*) AS item_count FROM content_comments cc ' . $sql;
            $count_array = $this->dbca->stmt_query($search_sql, $params_type, $params_array);

            return $count_array[0]->item_count;

        }catch (Exception $e){
            error_log($e->getMessage());
            return 0;
        }


    }


    /**
     * 查询商品评价信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchComments( &$arg_array ) {


        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //UUID
        if (isset($arg_array['uuid']) && is_string($arg_array['uuid'])) {
            $sql .= ' AND cc.uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND cc.pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //二级商品号
        if (isset($arg_array['twoPostID']) && is_numeric($arg_array['twoPostID'])) {
            $sql .= ' AND cc.twoPostID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['twoPostID'];
        }

        //处理状态 0:未回复 1:已回复
        if (isset($arg_array['commentStatus']) && in_array($arg_array['commentStatus'], array(0, 1))) {
            $sql .= ' AND cc.commentStatus = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['commentStatus'];
        }

        //开始时间
        if (isset($arg_array['startTime']) && !empty($arg_array['startTime'])) {
            $sql .= ' AND cc.createTime >= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['startTime'];
        }


        //结束时间
        if (isset($arg_array['endTime']) && !empty($arg_array['endTime'])) {
            $sql .= ' AND cc.createTime <= ?';
            $params_type .= 's';
            $params_array[] = $arg_array['endTime'];
        }

        //产品质量满意度
        if (isset($arg_array['grade']) && in_array($arg_array['grade'], array(1, 2, 3))) {
        	if($arg_array['grade'] == 1) {
                $sql .= ' AND cc.commentLevel BETWEEN ? AND ?';
	            $params_type .= 'ii';
	            $params_array[] = 4;
	            $params_array[] = 5;
        	}else if($arg_array['grade'] == 2){
                $sql .= ' AND cc.commentLevel BETWEEN ? AND ?';
	            $params_type .= 'ii';
	            $params_array[] = 2;
	            $params_array[] = 3;
        	}else{
        		$sql .= ' AND cc.commentLevel = ?';
	            $params_type .= 'i';
	            $params_array[] = 1;
        	}
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_comments cc ' . $sql;

                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;

                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;
				
                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;
                
                $arg_array['total'] = array();

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT cc.*,cm.custID,cm.head_pic,cm.rankName FROM content_comments cc
                           INNER JOIN cust_master cm ON cm.pcustID = cc.pcustID ' . $sql;

            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'custID' => $item->custID,
                    'head_pic' => set_cust_head_pic( $item->head_pic ),
                    'rankName' => $item->rankName,
                    'goods_name' => $item->goodsName,
                    'link' => set_goods_link($item->postID),
                    'img_link' => set_goods_img_link($item->goodsMainPic),
                    'createTime' => $item->createTime,
                    'info' => $item->commentInfo,
                    'ip' => $item->ipAddress,
                    'status' => $item->commentStatus,
                	'commentLevel' =>$item->commentLevel,
                    'answerInfo' => $item->answerInfo,
                    'answerUserID' => $item->answerUserID,
                    'answerDateTime' => $item->answerDateTime,
                    'satisfaction' => $item->satisfaction,
                	'uuid' => $item->uuid,
                	'displayFlg' => $item->displayFlg,
                	'recommendFlg' => $item->recommendFlg,
                	'commentStatus' => $item->commentStatus,
                	'commentInfo' => $item->commentInfo,
                	'creator' => $item->creator
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 查询投票信息
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['uuid']                  投票UUID
     * @param array $arg_array['vote_name']             投票名称
     * @param array $arg_array['vote_category']         投票分类
     * @param array $arg_array['date']                  显示标志
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null
     */
    public function searchVote( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票UUID
        if (isset($arg_array['uuid']) && !empty($arg_array['uuid'])) {
            $sql .= ' AND uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //投票名称
        if (isset($arg_array['vote_name']) && !empty($arg_array['vote_name'])) {
            $sql .= ' AND vote_name = ?';
            $params_type .= 's';
            $params_array[] = '%' . encrypt_paramD($arg_array['vote_name']) . '%';
        }

        //当前日期
        if (isset($arg_array['date']) && !empty($arg_array['date'])) {
            $sql .= ' AND start_time <= ? AND end_time >= ?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['date'];
            $params_array[] = $arg_array['date'];
        }

        //投票分类
        if (isset($arg_array['vote_category']) && !empty($arg_array['vote_category'])) {
            $sql .= ' AND vote_category = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['vote_category'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_vote ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_vote ' . $sql;;
            $item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询投票选择信息
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['vote_uuid']             投票uuid
     * @param array $arg_array['uuid']                  投票选项uuid
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null
     */
    public function searchVoteOption( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //投票uuid
        if (isset($arg_array['vote_uuid']) && !empty($arg_array['vote_uuid'])) {
            $sql .= ' AND vote_uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['vote_uuid'];
        }

        //投票选项uuid
        if (isset($arg_array['uuid']) && !empty($arg_array['uuid'])) {
            $sql .= ' AND uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }


        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS s_vote_option FROM s_vote ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_vote_option ' . $sql;;
            $item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 获得前台资源位
     * @param string $parent_index 顶级资源位索引
     * @return array|null
     */
    public function getShopSource( $parent_index ) {

        //查询资源位分类
        $arg_array = array(
            'category_index' => $parent_index,
            'useFlg' => 1
        );
        $source_category_array = $this->getSourceCategory( $arg_array );
        if (!$source_category_array)
            return null;

        //查询当前资源位分类下已发布的资源位信息
        $arg_array = array(
            'parent_id' => $source_category_array[0]['id'],
            'sortname' => 'csm.displayOrder',
            'sortorder' => 'ASC'
        );
        $publish_source_array = $this->getPublishSourceList( $arg_array );
        if (!$publish_source_array)
            return null;

        $source_array = array();

        $date = date(G_DEFAULT_DATE_FORMAT);

        foreach($publish_source_array as &$source_obj) {

            if ($source_obj->startDate && ($date < $source_obj->startDate || $date > $source_obj->endDate))
                continue;

            //如果商品资源位，则查询商品信息
            if ($source_obj->sourceStatus == 1) {
                $goods_obj = new entity('goods_sale');
                $goods_obj->postID = $source_obj->postID;
                if (!$this->dbca->fetch($goods_obj))
                    continue;
                if (empty($source_obj->pictureLink)) {
                    $source_obj->pictureLink = G_IMAGE_URL . '/goods/' . $goods_obj->goodsMainPic;
                } else {
                    $source_obj->pictureLink = G_IMAGE_URL . '/promotion/' . $source_obj->pictureLink;
                }
                $source_obj->salePrice = $goods_obj->salePrice;
                $source_obj->marketPrice = $goods_obj->marketPrice;
                if (G_SHOP_SUBSITE == 'shop') {
                    $source_obj->urlLink = set_goods_link( $source_obj->postID );
                } else {
                    $source_obj->urlLink = set_wap_goods_link( $source_obj->postID );
                }
            } else {
                $source_obj->salePrice = null;
                if (!empty($source_obj->pictureLink)) {
                    $source_obj->pictureLink = G_SHOP_CDN . '/quanliadmin/docroot/images/promotion/' . $source_obj->pictureLink;
                }
                if (!$source_obj->urlLink) {
                	if (G_SHOP_SUBSITE == 'shop') {
                		$source_obj->urlLink = '/' . G_SHOP_SUBSITE . '/display-' . $source_obj->source_id . '.html';
                	} else {
                		$source_obj->urlLink = '/' . G_SHOP_SUBSITE . '/topic.html?uid=' . $source_obj->source_id ;
                	}
                }
            }

            $source_array[$source_obj->source_category_index][] = array(
                'id' => $source_obj->source_id,
                'title' => $source_obj->source_title,
                'brief' => $source_obj->source_brief,
                'imgLink' => $source_obj->pictureLink,
                'link' => $source_obj->urlLink,
                'postID' => $source_obj->postID,
                'openPage' => $source_obj->openPage,
                'price' => $source_obj->salePrice,
                'marketPrice' => $source_obj->marketPrice,
            	'status' => $source_obj->sourceStatus,
            	'description' => $source_obj->description 
            );

        }

        return $source_array;


    }

    /**
     * 取消发布资源位
     * @param array $arg_array                          参数数组
     * @param array $arg_array['parent_id']             一级资源位分类ID
     * @param array $arg_array['category_id']           二级资源位分类ID
     * @param array $arg_array['source_id']             资源位ID
     * @return array
     */
    public function cancelPublishSource( $arg_array ) {

        if (!isset($arg_array['parent_id'], $arg_array['category_id'], $arg_array['source_id']) || !is_numeric($arg_array['source_id']) || !is_numeric($arg_array['category_id']) || !is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        try {

            $this->dbca->beginTransaction();

            //查询并检查已发布的资源位
            $content_source_publish = new entity('content_source_publish');
            $content_source_publish->top_category_id = $arg_array['parent_id'];
            $content_source_publish->source_category_id = $arg_array['category_id'];
            $content_source_publish->source_id = $arg_array['source_id'];
            if (!$this->dbca->fetch($content_source_publish))
                throw new Exception('资源位未发布', 404);
            $this->dbca->delete($content_source_publish);

            //查询并检查资源位信息
            $source_obj = new entity('content_source_manage');
            $source_obj->source_id = $arg_array['source_id'];
            $this->dbca->fetch($source_obj);
            $source_obj->publishFlg = 0;
            $source_obj->publisher = null;
            $source_obj->publishDate = null;
            $this->dbca->save($source_obj, 'content');

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => '取消发布成功' );

        } catch (Exception $e) {

            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 发布资源位
     * @param array $arg_array                          参数数组
     * @param array $arg_array['source_id']             资源位ID
     * @param array $arg_array['creator']               操作人
     * @param array $arg_array['createTime']            操作时间
     * @return array
     */
    public function publishSource( $arg_array ) {

        if (!isset($arg_array['source_id'], $arg_array['creator'], $arg_array['createTime']) || !is_numeric($arg_array['source_id']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        try {

            $this->dbca->beginTransaction();

            //查询并检查资源位信息
            $source_obj = new entity('content_source_manage');
            $source_obj->source_id = $arg_array['source_id'];
            if (!$this->dbca->fetch($source_obj))
                throw new Exception('资源位信息没有找到', 404);
            if ($source_obj->useFlg == 0)
                throw new Exception('资源位被禁用', 406);
            if ($source_obj->publishFlg == 1)
                throw new Exception('资源位已发布', 406);
            $source_obj->publishFlg = 1;
            $source_obj->publisher = $arg_array['creator'];
            $source_obj->publishDate = $arg_array['createTime'];
            $this->dbca->save($source_obj, 'content');

            //保存资源位发布信息
            $content_source_publish = new entity('content_source_publish');
            $content_source_publish->top_category_id = $source_obj->top_category_id;
            $content_source_publish->source_category_id = $source_obj->source_category_id;
            $content_source_publish->source_id = $source_obj->source_id;
            $content_source_publish->creator = $arg_array['creator'];
            $this->dbca->save($content_source_publish, 'content');

            $this->dbca->commit();

            return array( 'code' => 200, 'message' => '发布成功' );

        } catch (Exception $e) {

            $this->dbca->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 查询已发布的资源位
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['parent_id']             二级资源位分类ID
     * @param array $arg_array['category_id']           二级资源位分类ID
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null|array
     */
    public function getPublishSourceList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //一级资源位分类ID
        if (isset($arg_array['parent_id']) && is_numeric($arg_array['parent_id'])) {
            $sql .= ' AND csp.top_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['parent_id'];
        }

        //二级资源位分类ID
        if (isset($arg_array['category_id']) && is_numeric($arg_array['category_id'])) {
            $sql .= ' AND csp.source_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_id'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_source_publish csp ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT csm.*,csc.source_category_title,csc.source_category_index FROM  content_source_publish csp
                       INNER JOIN content_source_manage csm ON csm.source_id = csp.source_id
                       INNER JOIN content_source_category csc ON csc.source_category_id = csm.source_category_id ' . $sql;;
            $item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    /**
     * 设置内容状态
     *
     * @param array $arg_array
     */
    public function setContentStatus ( $arg_array ){
    	
    	if (!isset($arg_array['contentID'],$arg_array['useFlg']) || !is_numeric($_REQUEST['contentID']) || !in_array($arg_array['useFlg'], array('0','1'))) {
    		return array( 'code' => 400, 'message' => '参数错误' );
    	}
    	
    	$contentID = $arg_array['contentID']; //内容编号
    	$useFlg = $arg_array['useFlg'] == 1 ? 0 : 1;   //内容状态
    	
    	global $G_PGLOB;
    	
    	try {
    		
    		//判断内容是否存在
    		$content_center = new entity('content_center');
    		$content_center->contentID = $contentID;
    		if (!$G_PGLOB->DBCA->fetch($content_center)) {
    			throw new Exception('内容不存在', 1000);
    		}
    		
    		$content_center->useFlg = $useFlg;
    		$G_PGLOB->DBCA->save($content_center);
    		
    		if ($useFlg == 1) {
    			return array( 'code' => '200', 'message' => '启用成功' );
    		} else {
    			return array( 'code' => '200', 'message' => '禁用成功' );
    		}
    		
    	} catch (Exception $e) {
    		
    		if ($e->getCode() == 1000) {
    			return array( 'code' => 400, 'message' => $e->getMessage() );
    		} else {
    			return array( 'code' => 500, 'message' => '系统错误，请联系网站管理员');
    		}
    	}
    	
    }
    
	/**
	 * 删除内容中心内容
	 *
	 */
    public function deleteContent( $arg_array ){
    	
    	if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的内容' );
        
        $delete_array = explode('_', $arg_array['delete_str']);
        
        try {
			
            foreach($delete_array as &$source_id) {

                if (!is_numeric($source_id))
                    return array( 'code' => 400, 'message' => '参数错误' );
				
                //删除内容中心内容
                $content_center = new entity('content_center');
                $content_center->contentID = $source_id;
                if ($this->dbca->fetch($content_center)) {
                    $this->dbca->delete($content_center);
                }
				
            }
			
            return array( 'code' => 200, 'message' => '删除成功' );
			
        } catch (Exception $e) {
			
            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );
			
        }
        
    }
    /**
     * 删除资源位
     *
     * @param array $arg_array
     * @return unknown
     */
    public function deleteSource( $arg_array ) {

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的资源位' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$source_id) {

                if (!is_numeric($source_id))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询资源位信息
                $source_obj = new entity('content_source_manage');
                $source_obj->source_id = $source_id;
                if ($this->dbca->fetch($source_obj)) {
                    $this->dbca->delete($source_obj);
                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
    /**
     * 删除文章分类信息
     *
     * @param array $arg_array
     * @return array
     */
    public function deleteArticleCategory( $arg_array ) {

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的资源位' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$article_category_id) {

                if (!is_numeric($article_category_id))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //删除文章分类
                $article_obj = new entity('content_article_category');
                $article_obj->category_id = $article_category_id;
                if ($this->dbca->fetch($article_obj)) {
                    $this->dbca->delete($article_obj);

                    //删除分类下所有文章
                    $del_sql = 'DELETE FROM content_article WHERE article_category_id = ' . $article_category_id;
                    $this->dbca->execute($del_sql);

                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
	
    /**
     * 删除文章信息
     *
     * @param array $arg_array
     * @return array
     */
    public function deleteArticle( $arg_array ) {

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的资源位' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$article_id) {

                if (!is_numeric($article_id))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询资源位信息
                $article_obj = new entity('content_article');
                $article_obj->article_id = $article_id;
                if ($this->dbca->fetch($article_obj)) {
                    $this->dbca->delete($article_obj);
                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
    
    
    /**
     * 查询资源位分类信息
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['source_id']    资源位ID
     * @param array $arg_array['source_title'] 资源位标题
     * @param array $arg_array['useFlg']                使用标志
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null|array
     */
    public function getSource( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //资源位ID
        if (isset($arg_array['source_id']) && is_numeric($arg_array['source_id'])) {
            $sql .= ' AND csm.source_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['source_id'];
        }

        //资源位标题
        if (isset($arg_array['source_title']) && !empty($arg_array['source_title'])) {
            $sql .= ' AND csm.source_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['source_title'] . '%';
        }

        //一级资源位分类ID
        if (isset($arg_array['top_category_id']) && $arg_array['top_category_id'] > 0) {
            $sql .= ' AND csm.top_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['top_category_id'];
        }

        //二级资源位分类ID
        if (isset($arg_array['source_category_id']) && $arg_array['source_category_id'] > 0) {
            $sql .= ' AND csm.source_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['source_category_id'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND csm.useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //发布标志
        if (isset($arg_array['publishFlg']) && in_array($arg_array['publishFlg'], array(0, 1))) {
            $sql .= ' AND csm.publishFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['publishFlg'];
        }

        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_source_manage csm ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT csm.*,csc.source_category_title FROM content_source_manage csm
                           INNER JOIN content_source_category csc ON csc.source_category_id = csm.source_category_id ' . $sql;;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
						
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                //如果商品资源位，则查询商品信息
                if ($item->sourceStatus == 1) {
                    $goods_obj = new entity('goods_sale');
                    $goods_obj->postID = $item->postID;
                    if (!$this->dbca->fetch($goods_obj)){
                    	//continue;
                    }
                    $item->piclink = G_IMAGE_URL . '/goods/' . $goods_obj->goodsMainPic;
                    $item->salePrice = $goods_obj->salePrice;
                    $item->goodsName = $goods_obj->goodsName;
                    $item->product_code = $goods_obj->product_code;
                    $item->urlLink = '/shop/goods-' . $goods_obj->postID . '.html';
                } else {
                    $item->piclink = G_IMAGE_URL . '/promotion/' . $item->pictureLink;
                    $item->salePrice = null;
                    $item->goodsName = null;
                    $item->product_code = null;
                }

                $item_array[] = array(
                    'source_id' => $item->source_id,
                    'source_title' => $item->source_title,
                    'source_brief' => $item->source_brief,
                    'top_category_id' => $item->top_category_id,
                    'source_category_id' => $item->source_category_id,
                    'useFlg' => $item->useFlg,
                    'startDate' => $item->startDate,
                    'endDate' => $item->endDate,
                    'urlLink' => $item->urlLink,
                    'description' => isset($arg_array['source_id']) ? $item->description : null,
                    'pictureLink' => $item->pictureLink,
                    'sourceStatus' => $item->sourceStatus,
                    'publishFlg' => $item->publishFlg,
                    'openPage' => $item->openPage,
                    'source_category_title' => $item->source_category_title,
                    'piclink' => $item->piclink,
                    'link' => $item->urlLink,
                    'postID' => $item->postID,
                    'price' => $item->salePrice,
                    'goodsName' => $item->goodsName,
                    'product_code' => $item->product_code
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 添加或修改图片资源位信息
     * @param array $arg_array 资源位分类信息
     * @param array $arg_array['id']    资源位ID
     * @param array $arg_array['parent_id']    分类ID
     * @param array $arg_array['title']    标题
     * @param array $arg_array['brief']    资源位ALT
     * @param array $arg_array['url']    资源位外部URL
     * @param array $arg_array['description']    资源HTML内容
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['openPage']    是否打开新页面
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addPicSource( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['parent_id'], $arg_array['promotion_img'], $arg_array['title'], $arg_array['brief'], $arg_array['url'], $arg_array['useFlg'], $arg_array['openPage'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查资源位ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查资源位分类ID
        if (!is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '请选择资源位分类' );
        //检查资源位标题
        if (empty($arg_array['title']))
            return array( 'code' => 400, 'message' => '资源位标题不能为空' );
        //资源位URL 和 资源位HTML 不能同时为空
        if (empty($arg_array['url']) && empty($arg_array['description']))
            return array( 'code' => 400, 'message' => '资源位URL和资源位内容不能同时为空' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );
        //检查是否打开新页面
        if (!in_array($arg_array['openPage'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否打开新页面' );


        $commHandler = new commHandler();
        $arg_array['title'] = $commHandler->convertVar($arg_array['title']);
        $arg_array['url'] = $commHandler->convertVar($arg_array['url']);
        $arg_array['brief'] = $commHandler->convertVar($arg_array['brief']);
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

        try {

            $source_obj = new entity('content_source_manage');

            //如果资源位ID不为空，则检查资源位是否存在
            if ($arg_array['id'] > 0) {
                $source_obj->source_id = $arg_array['id'];
                if (!$this->dbca->fetch($source_obj))
                    return array( 'code' => 404, 'message' => '资源位信息没有找到' );
                $source_obj->editor = $arg_array['creator'];
            } else {
                $source_obj->creator = $arg_array['creator'];
            }

            //检查资源位分类信息
            $sc_arg_array = array(
                'category_id' => $_REQUEST['parent_id']
            );
            $source_category_array = $this->getSourceCategory( $sc_arg_array );
            if (!$source_category_array)
                return array( 'code' => 404, 'message' => '资源位分类信息没有找到' );

            //保存资源位信息
            $source_obj->source_title = $arg_array['title'];
            $source_obj->source_brief = $arg_array['brief'];
            $source_obj->urlLink = $_REQUEST['url'];
            $source_obj->description = $_REQUEST['description'];
            if (!empty($_REQUEST['promotion_img']))
                $source_obj->pictureLink = $_REQUEST['promotion_img'];
            $source_obj->top_category_id = $source_category_array[0]['parent_id'];
            $source_obj->source_category_id = $source_category_array[0]['id'];
            $source_obj->sourceStatus = 2;
            $source_obj->publishFlg = 0;
            $source_obj->useFlg = $_REQUEST['useFlg'];
            $source_obj->openPage = $_REQUEST['openPage'];

            $this->dbca->save($source_obj, 'content');

            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
    
    /**
     * 添加或修改富文本资源位信息
     * @param array $arg_array 资源位分类信息
     * @param array $arg_array['id']    资源位ID
     * @param array $arg_array['parent_id']    分类ID
     * @param array $arg_array['title']    标题
     * @param array $arg_array['brief']    资源位ALT
     * @param array $arg_array['url']    资源位外部URL
     * @param array $arg_array['description']    资源HTML内容
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['openPage']    是否打开新页面
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addRichTextSource( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['parent_id'], $arg_array['brief'], $arg_array['useFlg'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查资源位ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查资源位分类ID
        if (!is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '请选择资源位分类' );
        //资源位HTML不能为空
        if (empty($arg_array['description']))
            return array( 'code' => 400, 'message' => '资源位内容不能为空' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );
		
        $commHandler = new commHandler();
        $arg_array['brief'] = $commHandler->convertVar($arg_array['brief']);
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

        try {

            $source_obj = new entity('content_source_manage');

            //如果资源位ID不为空，则检查资源位是否存在
            if ($arg_array['id'] > 0) {
                $source_obj->source_id = $arg_array['id'];
                if (!$this->dbca->fetch($source_obj))
                    return array( 'code' => 404, 'message' => '资源位信息没有找到' );
                $source_obj->editor = $arg_array['creator'];
            } else {
                $source_obj->creator = $arg_array['creator'];
            }

            //检查资源位分类信息
            $sc_arg_array = array(
                'category_id' => $_REQUEST['parent_id']
            );
            $source_category_array = $this->getSourceCategory( $sc_arg_array );
            if (!$source_category_array)
                return array( 'code' => 404, 'message' => '资源位分类信息没有找到' );

            //保存资源位信息
            $source_obj->source_brief = $arg_array['brief'];
            $source_obj->description = $_REQUEST['description'];
            $source_obj->top_category_id = $source_category_array[0]['parent_id'];
            $source_obj->source_category_id = $source_category_array[0]['id'];
            $source_obj->sourceStatus = 3; //富文本资源位标记
            $source_obj->publishFlg = 0;
            $source_obj->useFlg = $_REQUEST['useFlg'];
			
            $this->dbca->save($source_obj, 'content');

            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
    
    /**
     * 添加或修改文本资源位信息
     * @param array $arg_array 资源位分类信息
     * @param array $arg_array['id']    资源位ID
     * @param array $arg_array['parent_id']    分类ID
     * @param array $arg_array['title']    标题
     * @param array $arg_array['brief']    资源位ALT
     * @param array $arg_array['url']    资源位外部URL
     * @param array $arg_array['description']    资源HTML内容
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['openPage']    是否打开新页面
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addTextSource( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['parent_id'], $arg_array['title'], $arg_array['brief'], $arg_array['url'], $arg_array['useFlg'], $arg_array['openPage'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查资源位ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查资源位分类ID
        if (!is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '请选择资源位分类' );
        //检查资源位标题
        if (empty($arg_array['title']))
            return array( 'code' => 400, 'message' => '资源位标题不能为空' );
        //资源位URL 和 资源位HTML 不能同时为空
        if (empty($arg_array['url']) && empty($arg_array['description']))
            return array( 'code' => 400, 'message' => '资源位URL和资源位内容不能同时为空' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );
        //检查是否打开新页面
        if (!in_array($arg_array['openPage'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否打开新页面' );


        $commHandler = new commHandler();
        $arg_array['title'] = $commHandler->convertVar($arg_array['title']);
        $arg_array['url'] = $commHandler->convertVar($arg_array['url']);
        $arg_array['brief'] = $commHandler->convertVar($arg_array['brief']);
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

        try {

            $source_obj = new entity('content_source_manage');

            //如果资源位ID不为空，则检查资源位是否存在
            if ($arg_array['id'] > 0) {
                $source_obj->source_id = $arg_array['id'];
                if (!$this->dbca->fetch($source_obj))
                    return array( 'code' => 404, 'message' => '资源位信息没有找到' );
                $source_obj->editor = $arg_array['creator'];
            } else {
                $source_obj->creator = $arg_array['creator'];
            }

            //检查资源位分类信息
            $sc_arg_array = array(
                'category_id' => $_REQUEST['parent_id']
            );
            $source_category_array = $this->getSourceCategory( $sc_arg_array );
            if (!$source_category_array)
                return array( 'code' => 404, 'message' => '资源位分类信息没有找到' );

            //保存资源位信息
            $source_obj->source_title = $arg_array['title'];
            $source_obj->source_brief = $arg_array['brief'];
            $source_obj->urlLink = $_REQUEST['url'];
            $source_obj->description = $_REQUEST['description'];
            if (!empty($_REQUEST['promotion_img']))
                $source_obj->pictureLink = $_REQUEST['promotion_img'];
            $source_obj->top_category_id = $source_category_array[0]['parent_id'];
            $source_obj->source_category_id = $source_category_array[0]['id'];
            $source_obj->sourceStatus = 4; //文本资源位标记
            $source_obj->publishFlg = 0;
            $source_obj->useFlg = $_REQUEST['useFlg'];
            $source_obj->openPage = $_REQUEST['openPage'];

            $this->dbca->save($source_obj, 'content');

            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }

    /**
     * 添加或修改商品资源位信息
     * @param array $arg_array 资源位分类信息
     * @param array $arg_array['id']    资源位ID
     * @param array $arg_array['parent_id']    分类ID
     * @param array $arg_array['product_code']    商品编码
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['openPage']    是否打开新页面
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addGoodsSource($arg_array) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['useFlg'], $arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查资源位ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查资源位分类ID
        if (!is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '请选择资源位分类' );
        //商品编码
        if (!isset($_REQUEST['product_code']) || empty($_REQUEST['product_code']))
            return array( 'code' => 400, 'message' => '商品编码不能为空' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );
        //检查是否打开新页面
        if (!in_array($arg_array['openPage'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否打开新页面' );

        try {

            $source_obj = new entity('content_source_manage');

            //如果资源位ID不为空，则检查资源位是否存在
            if ($arg_array['id'] > 0) {
                $source_obj->source_id = $arg_array['id'];
                if (!$this->dbca->fetch($source_obj))
                    return array( 'code' => 404, 'message' => '资源位信息没有找到' );
                $source_obj->editor = $arg_array['creator'];
            } else {
                $source_obj->creator = $arg_array['creator'];
            }

            //检查资源位分类信息
            $sc_arg_array = array(
                'category_id' => $_REQUEST['parent_id']
            );
            $source_category_array = $this->getSourceCategory( $sc_arg_array );
            if (!$source_category_array)
                return array( 'code' => 404, 'message' => '资源位分类信息没有找到' );


            //查询商品信息
            $search_sql = 'SELECT * FROM goods_sale WHERE product_code = ?';
            $goods_array = &$this->dbca->stmt_query($search_sql, 's', $_REQUEST['product_code']);
            if (!$goods_array)
                return array( 'code' => 404, 'message' => '商品信息没有找到' );

            //保存资源位信息
            $source_obj->source_title = $goods_array[0]->goodsName;
            $source_obj->source_brief = $goods_array[0]->goodsName;
            if (isset($_REQUEST['promotion_img']) && !empty($_REQUEST['promotion_img']))
                $source_obj->pictureLink = $_REQUEST['promotion_img'];
            $source_obj->postID = $goods_array[0]->postID;
            $source_obj->top_category_id = $source_category_array[0]['parent_id'];
            $source_obj->source_category_id = $source_category_array[0]['id'];
            $source_obj->sourceStatus = 1;
            $source_obj->publishFlg = 0;
            $source_obj->useFlg = $_REQUEST['useFlg'];
            $source_obj->openPage = $_REQUEST['openPage'];

            $this->dbca->save($source_obj, 'content');

            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }
	
    /**
     * 添加或修改文章信息
     *
     * @param array $arg_array 文章信息数组
     * 
     */
	function saveArticleInfo ($arg_array) {

        //检查是否缺少参数
        if (!isset($arg_array['creator'], $arg_array['article_id'], $arg_array['article_title'], $arg_array['category_id'], $arg_array['article_link'], $arg_array['article_summary'], $arg_array['useFlg'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        //检查标题
        if (empty($arg_array['article_title']))
            return array( 'code' => 400, 'message' => '请填写文章标题' );

        //检查分类
        if (!is_numeric($arg_array['category_id']) || $arg_array['category_id'] <= 0)
            return array( 'code' => 400, 'message' => '请选择文章分类' );

        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择使用状态' );

        //检查文章ID
        if (empty($arg_array['article_id']) || !is_numeric($arg_array['article_id']) || $arg_array['article_id'] < 0)
            $arg_array['article_id'] = 0;

        //实例化公共类
        $commHandler = new commHandler();

        $arg_array['article_title'] = $commHandler->convertVar($arg_array['article_title']);
        $arg_array['article_link'] = $commHandler->convertVar($arg_array['article_link']);
        $arg_array['article_summary'] = $commHandler->convertVar($arg_array['article_summary']);
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

		try {

            //查询文章分类信息
            $c_arg_array = array(
                'category_id' => $arg_array['category_id']
            );
            $article_category_array = $this->searchArticleCategory( $c_arg_array );
            if (!$article_category_array)
                return array( 'code' => 404, 'message' => '文章分类信息没有找到' );

            //添加或修改文章信息
            $article_obj = new entity('content_article');
            if ($arg_array['article_id'] > 0) {
                $article_obj->article_id = $arg_array['article_id'];
                if (!$this->dbca->fetch($article_obj))
                    return array( 'code' => 404, 'message' => '文章信息没有找到' );
                $article_obj->editor = $arg_array['creator'];
            } else {
                $article_obj->creator = $arg_array['creator'];
                $article_obj->article_date = date('Ym');
            }
            $article_obj->article_title = $arg_array['article_title'];
            $article_obj->useFlg = $arg_array['useFlg'];
            if (isset($arg_array['commentFlg'])) {
                $article_obj->commentFlg = $arg_array['commentFlg'];
            }
            if (isset($arg_array['article_pic'])) {
                $article_obj->commentFlg = $arg_array['article_pic'];
            }
            $article_obj->article_link = $arg_array['article_link'];
            $article_obj->article_summary = $arg_array['article_summary'];
            $article_obj->description = $arg_array['description'];
            $article_obj->article_category_id = $article_category_array[0]['category_id'];
            $article_obj->article_category_index = $article_category_array[0]['category_index'];
            $article_obj->article_category_name = $article_category_array[0]['category_title'];
            $article_obj->article_type = $article_category_array[0]['category_type'];
            $this->dbca->save($article_obj);

			if ($arg_array['operFlg'] == 4) {
				return array('code' => 200 , 'message' => '文章保存成功');
			} else {
				return array('code' => 200 , 'message' => '文章修改成功');
			}
				
		} catch (Exception $e){
			error_log($e->getMessage());
			return array('code' => 500, 'message' => '系统错误');
		}
	}


    /**
     * 添加或修改资源位分类信息
     * @param array $arg_array 资源位分类信息
     * @param array $arg_array['id']    分类ID
     * @param array $arg_array['index']    分类别名
     * @param array $arg_array['title']    分类标题
     * @param array $arg_array['parent_id']    上级分类ID
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['description']    备注
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addSourceCategory( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['index'], $arg_array['title'], $arg_array['parent_id'], $arg_array['useFlg'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查分类ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查分类别名
        if (empty($arg_array['index']))
            return array( 'code' => 400, 'message' => '分类别名不能为空' );
        //检查分类标题
        if (empty($arg_array['title']))
            return array( 'code' => 400, 'message' => '分类标题不能为空' );
        //检查上级分类ID
        if (!is_numeric($arg_array['parent_id']))
            return array( 'code' => 400, 'message' => '请选择上级分类' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );

        $commHandler = new commHandler();
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

        try {

            $source_obj = new entity('content_source_category');
            if ($arg_array['id'] > 0) {
                $source_obj->source_category_id = $arg_array['id'];
                if (!$this->dbca->fetch($source_obj))
                    return array( 'code' => 404, 'message' => '资源位分类信息没有找到' );
                $source_obj->editor = $arg_array['creator'];
            } else {
                $source_obj->creator = $arg_array['creator'];
            }
            $source_obj->source_category_index = $arg_array['index'];
            $source_obj->source_category_title = $arg_array['title'];
            $source_obj->description = $arg_array['description'];
            $source_obj->parent_category_id = $arg_array['parent_id'];
            $source_obj->useFlg = $arg_array['useFlg'];
            $this->dbca->save($source_obj, 'content');

            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }


    }

    /**
     * 查询资源位分类信息
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['source_category_id']    分类ID
     * @param array $arg_array['source_category_index'] 分类别名
     * @param array $arg_array['source_category_title'] 分类标题
     * @param array $arg_array['parent_category_id']    分类父级ID
     * @param array $arg_array['useFlg']                使用标志
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null|array
     */
    public function getSourceCategory( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //分类ID
        if (isset($arg_array['category_id']) && is_numeric($arg_array['category_id'])) {
            $sql .= ' AND source_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_id'];
        }

        //分类别名
        if (isset($arg_array['category_index']) && !empty($arg_array['category_index'])) {
            $sql .= ' AND source_category_index = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['category_index'];
        }

        //分类标题
        if (isset($arg_array['category_title']) && !empty($arg_array['category_title'])) {
            $sql .= ' AND source_category_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['category_title'] . '%';
        }

        //分类父级ID
        if (isset($arg_array['parent_id']) && is_numeric($arg_array['parent_id'])) {
            $sql .= ' AND parent_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['parent_id'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_source_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_source_category ' . $sql;;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'createTime' => $item->createTime,
                    'creator' => $item->creator,
                    'id' => $item->source_category_id,
                    'index' => $item->source_category_index,
                    'title' => $item->source_category_title,
                    'description' => $item->description,
                    'parent_id' => $item->parent_category_id,
                    'useFlg' => $item->useFlg
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }


    /**
     * 删除资源位分类信息
     * @param array $arg_array 参数数组
     * $arg_array = array(
            'delete_str' => string 资源位分类ID字符串 11_12_13_14 || 11
     * )
     * @return array
     */
    public function deleteSourceCategory( $arg_array ) {

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的资源位分类' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$category_id) {

                if (!is_numeric($category_id) || $category_id <= 0)
                    return array( 'code' => 400, 'message' => '参数错误' );

                //删除资源位分类
                $category_obj = new entity('content_source_category');
                $category_obj->source_category_id = $category_id;
                if ($this->dbca->fetch($category_obj)) {

                    //如果该资源位分类是顶级资源位分类，则删除所有下级资源位分类
                    if ($category_obj->parent_category_id == 0) {

                        $del_sql = 'DELETE FROM content_source_category WHERE parent_category_id = ' . $category_id;
                        $this->dbca->execute($del_sql);

                        //删除分类下的所有资源位
                        $del_sql = 'DELETE FROM content_source_manage WHERE top_category_id = ' . $category_id;
                        $this->dbca->execute($del_sql);

                        //删除分类下的所有已发布的资源位
                        $del_sql = 'DELETE FROM content_source_publish WHERE top_category_id = ' . $category_id;
                        $this->dbca->execute($del_sql);

                    } else {

                        //删除分类下的所有资源位
                        $del_sql = 'DELETE FROM content_source_manage WHERE source_category_id = ' . $category_id;
                        $this->dbca->execute($del_sql);

                        //删除分类下的所有已发布的资源位
                        $del_sql = 'DELETE FROM content_source_publish WHERE source_category_id = ' . $category_id;
                        $this->dbca->execute($del_sql);

                    }

                    //删除当前资源位分类
                    $this->dbca->delete($category_obj);

                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }


    
/****************************************************投票管理start********************************************************/
    /*
     * @abstract      searchVoteInfo()      查询投票
    * @param         $importdata = array(可选：page,rp,sortname,sortorder,vote_category,start_time,end_time)
    * @return        $return_data('code','message'=>('data','total'));
    *  */
    public function searchVoteInfo ($importData=array()) {
    	//返回数组
    	$return_data = array();
    		
    	//错误信息
    	$error = '';
    
    	/* 传入数据有效性验证start */
    	if (isset($importData['page']) && !empty($importData['page']) && !is_numeric($importData['page'])){
    		$error .= '当前页码必须为数字<br/>';
    	}
    
    	if (isset($importData['rp']) && !empty($importData['rp']) && !is_numeric($importData['rp'])){
    		$error .= '每页记录数必须为数字<br/>';
    	}
    
    	if (isset($importData['sortname']) && !empty($importData['sortname']) && !is_string($importData['sortname'])){
    		$error .= '排序方式必须为字符串<br/>';
    	}
    
    	if (isset($importData['sortorder']) && !empty($importData['sortorder']) && !is_string($importData['sortorder'])){
    		$error .= '排序方式必须为字符串<br/>';
    	}
    		
    	if (isset($importData['vote_category']) && !empty($importData['vote_category']) && !is_numeric($importData['vote_category'])){
    		$error .= '投票分类必须为数字<br/>';
    	}
    
    	if (isset($importData['start_time']) && !empty($importData['start_time']) && !is_string($importData['start_time'])){
    		$error .= '投票开始时间必须为字符串<br/>';
    	}
    
    	if (isset($importData['end_time']) && !empty($importData['end_time']) && !is_string($importData['end_time'])){
    		$error .= '投票结束时间必须为字符串<br/>';
    	}
    
    	if (!empty($error)){
    		$return_data['code'] = 400;
    		$return_data['message']['total'] = 0;
    		$return_data['message']['data'] = $error;
    		return $return_data;
    	}
    	/* 传入数据有效性验证end */
    
    	$page = isset($importData['page']) ? trim($importData['page']) : '';//当前页
    	$rp = isset($importData['rp']) ? trim($importData['rp']) : '';//每页记录数
    	$sortname = isset($importData['sortname']) ? trim($importData['sortname']) : 'timeStamp';//排序字段名
    	$sortorder = isset($importData['sortorder']) ? trim($importData['sortorder']) : 'DESC';//排序方式
    	$start_time = isset($importData['start_time']) ? $importData['start_time'] : '';//投票开始时间
    	$end_time = isset($importData['end_time']) ? $importData['end_time'] : '';//投票开始时间
    	$vote_category = isset($importData['vote_category']) ? $importData['vote_category'] : '';//投票分类
    
    	//求总记录数
    	$sql_count = 'SELECT COUNT(uuid) countNum FROM content_vote';
    
    	//查询SQL语句（注：表中的sortOrder另命名为orderNum）
    	$sql_search = 'SELECT * FROM content_vote';
    
    	//排序
    	$str_order = '';
    
    	//分页
    	$str_limit = '';
    
    	//查询条件
    	$str_search = ' WHERE';
    
    	//查询参数类型
    	$type_search = '';
    
    	//查询参数数组
    	$param_search = array();
    
    	//查询条件：投票uuid
    	if (!empty($vote_category)){
    		$str_search .= ' AND vote_category = ?';
    		$type_search .= 'i';
    		$param_search[] = $vote_category;
    	}
    		
    	//查询条件：投票开始时间
    	if (!empty($start_time)){
    		$str_search .= ' AND start_time <= ?';
    		$type_search .= 's';
    		$param_search[] = $start_time;
    	}
    
    	//查询条件：投票结束时间
    	if (!empty($end_time)){
    		$str_search .= ' AND end_time >= ?';
    		$type_search .= 's';
    		$param_search[] = $end_time;
    	}
    
    	//分页
    	if (!empty($page) && !empty($rp)){
    		$str_limit = sprintf(' LIMIT %d, %d', ($page - 1) * $rp, $rp);
    	}
    
    	//排序
    	if (!empty($sortname) && !empty($sortorder)){
    		$str_order = sprintf(' ORDER BY %s %s', $sortname, $sortorder);
    	}
    
    	//组装SQL语句
    	if (!empty($type_search)){
    		$str_search = str_replace('WHERE AND', 'WHERE', $str_search);
    		$sql_count = $sql_count.$str_search;
    		$sql_search = $sql_search.$str_search.$str_order.$str_limit;
    	}else {
    		$sql_search = $sql_search.$str_order.$str_limit;
    	}
    
    	$value_count = empty($type_search) ? $this->dbca->stmt_query($sql_count) : $this->dbca->stmt_query($sql_count,$type_search,$param_search);
    
    	if ($value_count){
    
    		$return_data['code'] = 200;
    
    		//总记录条数
    		$return_data['message']['total'] = $value_count[0]->countNum;
    
    		//得到查询结果
    		$return_data['message']['data'] = empty($type_search) ? $this->dbca->stmt_query($sql_search) : $this->dbca->stmt_query($sql_search,$type_search,$param_search);
    
    	}else {
    		$return_data['code'] = 200;
    		$return_data['message']['total'] = 0;
    		$return_data['message']['data'] = '';
    	}
    
    	return $return_data;
    }
    
    /*
     * @abstract      editVote()      编辑投票
    * @param         $importdata = array(必传：operFlg,vote_category,vote_name,start_time,end_time,can_multi,options
    	 * 									     可选：creator（新增必传）,editor（编辑必传）,vote_uuid（编辑必传）)
    * @return        array('code','message')
    *  */
    public function editVote ( $importData ) {
    	$error = '';
    	$flg = '';//判断新选项是否与原选项相同
    	/* 传入数据有效性验证start */
    	//没有传入数据
    	if (empty($importData)) $error .= '没有传入任何数据<br/>';
    
    	if (!isset($importData['operFlg'])){
    		$error .= '未传入operFlg<br/>';
    	}
    
    	//operFlg：2，新增；3，修改
    	if ($importData['operFlg'] != 2 && $importData['operFlg'] != 3) {
    		$error .= '传入operFlg错误<br/>';
    	}
    
    	if ($importData['operFlg'] == 2 && !isset($importData['creator'])) {
    		$error .= '创建人不能为空<br/>';
    	}
    
    	if ($importData['operFlg'] == 3 && !isset($importData['editor'])) {
    		$error .= '修改人不能为空<br/>';
    	}
    
    	if (empty($importData['vote_category']) || !is_numeric($importData['vote_category'])) {
    		$error .= '投票分类不能为空且必须为数字<br/>';
    	}
    
    	if (empty($importData['vote_name']) || !is_string($importData['vote_name'])) {
    		$error .= '投票主题不能为空且必须为字符串<br/>';
    	}
    
    	if (empty($importData['start_time']) || !is_string($importData['start_time'])) {
    		$error .= '开始时间不能为空且必须为字符串<br/>';
    	}
    
    	if (empty($importData['end_time']) || !is_string($importData['end_time'])) {
    		$error .= '结束时间不能为空且必须为字符串<br/>';
    	}
    
    	if (!isset($importData['can_multi']) || !is_numeric($importData['can_multi'])) {
    		$error .= '是否多选不能为空且传值为数字<br/>';
    	}
    
    	if (empty($importData['options']) || !is_array($importData['options'])) {
    		$error .= '投票选项不能为空且传值必须为数组<br/>';
    	}
    
    	if ($importData['operFlg'] == 3 && !isset($importData['vote_uuid'])) {
    		$error .= '原投票uuid传值错误<br/>';
    	}
    
    	if (!empty($error)){
    		return array('code' => 400, 'message' => $error);
    	}
    	/* 传入数据有效性验证end */
    
    	$operFlg = $importData['operFlg'];//operFlg
    	$creator = $importData['creator'];//创建人
    	$editor = $importData['editor'];//修改人
    	$vote_category = trim($importData['vote_category']);//投票分类
    	$vote_name = trim($importData['vote_name']);//投票主题
    	$start_time = trim($importData['start_time']);//开始时间
    	$end_time = trim($importData['end_time']);//结束时间
    	$can_multi = trim($importData['can_multi']);//是否可多选
    	$vote_uuid = (!empty($importData['vote_uuid'])) ? trim($importData['vote_uuid']) : null;//原投票uuid
    	$options = $importData['options'];//投票选项
    
    	//将传入的options去重复，如果去重复后的值与原值不同，则返回错误信息
    	$options_check = array_unique($options);
    	if ($options_check != $options){
    		return array('code' => 400, 'message' => '投票选项有重复');
    	}
    
    	//判断是否被删除，同时将原投票选项的选项名组成数组$originalOptions，在后面的保存进数据库环节，
    	//如果$originalOptions==$options，则不跟新content_vote_option，否则会先删除原投票选项，再把新的选项插入
    	if ($operFlg == 3 && !empty($vote_uuid)){
    		$result_vote = self::searchAllVoteInfo(array('vote_uuid' => $vote_uuid));
    		if ($result_vote['code'] != 200 || empty($result_vote['message']['data'])){
    			return array('code' => 400, 'message' => '该投票不存在或被删除');
    		}else {
    			$originalOptions = array();
    			$flg = '';//flg=1表示新的投票选项有不在原有选项中，flg=2表示新选项与原选项相同
    			foreach ($result_vote['message']['data'] as $voteItem){
    				$originalOptions[] = $voteItem->option_name;
    			}
    
    			if (!empty($options)){
    				foreach ($options as $optionList){
    					if (!in_array($optionList, $originalOptions)){
    						$flg = 1;
    					}
    				}
    				if ((count($options) == count($originalOptions)) && $flg != 1){
    					$flg = 2;
    				}
    			}
    		}
    	}
    
    	try {
    			
    		if ($operFlg == 2){//新增
    
    			$this->dbca->beginTransaction();
    
    			$uuid = make_uuid();
    
    			$sql_insert = 'INSERT INTO content_vote(uuid,timeStamp,creator,createTime,vote_category,vote_name,start_time,end_time,can_multi) VALUES (?,?,?,?,?,?,?,?,?)';
    			$type_insert = 'ssssisssi';
    			$param_insert = array($uuid,date('Y-m-d H:i:s'),$creator,date('Y-m-d H:i:s'),$vote_category,$vote_name,$start_time,$end_time,$can_multi);
    			$this->dbca->stmt_query($sql_insert,$type_insert,$param_insert);
    
    			if (!empty($options)){
    
    				for ($i=0; $i<count($options); $i++){//循环保存新选项
    					$content_vote_option = new entity('content_vote_option');
    					$content_vote_option->creator = $creator;
    					$content_vote_option->createTime = date('Y-m-d H:i:s');
    					$content_vote_option->vote_uuid = $uuid;
    					$content_vote_option->option_name = $options[$i];
    					$content_vote_option->option_sortBy = $i+1;
    					$this->dbca->save($content_vote_option,'content');
    				}
    
    			}
    
    			$this->dbca->commit();
    
    		}elseif ($operFlg == 3){//修改
    
    			$content_vote = new entity('content_vote');
    				
    			$content_vote->uuid = $vote_uuid;
    				
    			if ($this->dbca->fetch($content_vote)){
    
    				$content_vote->timeStamp = date('Y-m-d H:i:s');
    				$content_vote->editor = $editor;
    				$content_vote->vote_category = $vote_category;
    				$content_vote->vote_name = $vote_name;
    				$content_vote->start_time = $start_time;
    				$content_vote->end_time = $end_time;
    				$content_vote->can_multi = $can_multi;
    
    				//如选项未改变
    				if (!empty($flg) && $flg == 2){
    						
    					$this->dbca->save($content_vote,'content');
    						
    				}else {
    						
    					$sql_del = 'DELETE FROM content_vote_option WHERE vote_uuid = ?';
    						
    					$this->dbca->beginTransaction();
    
    					$this->dbca->save($content_vote,'content');//保存投票
    
    					$this->dbca->stmt_query($sql_del, 's', $vote_uuid);//删除原选项
    						
    					if (!empty($options)){
    
    						for ($i=0; $i<count($options); $i++){//循环保存新选项
    							$content_vote_option = new entity('content_vote_option');
    							$content_vote_option->creator = $creator;
    							$content_vote_option->createTime = date('Y-m-d H:i:s');
    							$content_vote_option->vote_uuid = $vote_uuid;
    							$content_vote_option->option_name = $options[$i];
    							$content_vote_option->option_sortBy = $i+1;
    							$this->dbca->save($content_vote_option,'content');
    						}
    
    					}
    
    					$this->dbca->commit();
    				}
    
    			}
    		}
    
    		if ($operFlg == 2){
    			$msg = '新增投票成功';
    		}elseif ($operFlg == 3){
    			$msg = '修改投票成功';
    		}
    			
    		return array('code'=>200,'message'=>$msg);
    			
    	}catch (Exception $e) {
    		$this->dbca->rollBack();
    		$msg = $e->getMessage();
    		return array('code'=>500,'message'=>$msg);
    	}
    
    }
    
    /*
     * @abstract       searchAllVoteInfo()    获得全部投票信息（含投票选项）
    * @param          $importData
    * @return         $return_data('code','message'=>('data','total'));
    *  */
    public function searchAllVoteInfo ($importData=array()) {
    
    	//返回数组
    	$return_data = array();
    		
    	//错误信息
    	$error = '';
    
    	/* 传入数据有效性验证start */
    	if (isset($importData['page']) && !empty($importData['page']) && !is_numeric($importData['page'])){
    		$error .= '当前页码必须为数字<br/>';
    	}
    
    	if (isset($importData['rp']) && !empty($importData['rp']) && !is_numeric($importData['rp'])){
    		$error .= '每页记录数必须为数字<br/>';
    	}
    
    	if (isset($importData['sortname']) && !empty($importData['sortname']) && !is_string($importData['sortname'])){
    		$error .= '排序方式必须为字符串<br/>';
    	}
    
    	if (isset($importData['sortorder']) && !empty($importData['sortorder']) && !is_string($importData['sortorder'])){
    		$error .= '排序方式必须为字符串<br/>';
    	}
    		
    	if (isset($importData['vote_category']) && !empty($importData['vote_category']) && !is_numeric($importData['vote_category'])){
    		$error .= '投票分类必须为数字<br/>';
    	}
    
    	if (isset($importData['start_time']) && !empty($importData['start_time']) && !is_string($importData['start_time'])){
    		$error .= '投票开始时间必须为字符串<br/>';
    	}
    
    	if (isset($importData['end_time']) && !empty($importData['end_time']) && !is_string($importData['end_time'])){
    		$error .= '投票结束时间必须为字符串<br/>';
    	}
    
    	if (isset($importData['vote_uuid']) && !empty($importData['vote_uuid']) && !is_string($importData['vote_uuid'])){
    		$error .= '投票uuid必须为字符串<br/>';
    	}
    
    	if (!empty($error)){
    		$return_data['code'] = 400;
    		$return_data['message']['total'] = 0;
    		$return_data['message']['data'] = $error;
    		return $return_data;
    	}
    	/* 传入数据有效性验证end */
    
    	$page = isset($importData['page']) ? trim($importData['page']) : '';//当前页
    	$rp = isset($importData['rp']) ? trim($importData['rp']) : '';//每页记录数
    	$sortname = isset($importData['sortname']) ? trim($importData['sortname']) : 'cv.timeStamp';//排序字段名
    	$sortorder = isset($importData['sortorder']) ? trim($importData['sortorder']) : 'DESC';//排序方式
    	$start_time = isset($importData['start_time']) ? trim($importData['start_time']) : '';//投票开始时间
    	$end_time = isset($importData['end_time']) ? trim($importData['end_time']) : '';//投票开始时间
    	$vote_category = isset($importData['vote_category']) ? trim($importData['vote_category']) : '';//投票分类
    	$vote_uuid = isset($importData['vote_uuid']) ? trim($importData['vote_uuid']) : '';//投票uuid
    
    	//求总记录数
    	$sql_count = 'SELECT COUNT(cv.uuid) countNum FROM content_vote cv';
    
    	//查询SQL语句（注：表中的sortOrder另命名为orderNum）
    	$sql_search = 'SELECT cv.*, cvo.uuid option_uuid, cvo.option_name, cvo.option_count, cvo.option_sortBy, cvo.vote_uuid FROM content_vote cv
					   LEFT JOIN content_vote_option cvo ON cv.uuid = cvo.vote_uuid';
    
    	//排序
    	$str_order = '';
    
    	//分页
    	$str_limit = '';
    
    	//查询条件
    	$str_search = ' WHERE';
    
    	//查询参数类型
    	$type_search = '';
    
    	//查询参数数组
    	$param_search = array();
    
    	//查询条件：投票分类
    	if (!empty($vote_category)){
    		$str_search .= ' AND cv.vote_category = ?';
    		$type_search .= 'i';
    		$param_search[] = $vote_category;
    	}
    
    	//查询条件：投票uuid
    	if (!empty($vote_uuid)){
    		$str_search .= ' AND cv.uuid = ?';
    		$type_search .= 's';
    		$param_search[] = $vote_uuid;
    	}
    		
    	//查询条件：投票开始时间
    	if (!empty($start_time)){
    		$str_search .= ' AND cv.start_time <= ?';
    		$type_search .= 's';
    		$param_search[] = $start_time;
    	}
    
    	//查询条件：投票结束时间
    	if (!empty($end_time)){
    		$str_search .= ' AND cv.end_time >= ?';
    		$type_search .= 's';
    		$param_search[] = $end_time;
    	}
    
    	//分页
    	if (!empty($page) && !empty($rp)){
    		$str_limit = sprintf(' LIMIT %d, %d', ($page - 1) * $rp, $rp);
    	}
    
    	//排序
    	if (!empty($sortname) && !empty($sortorder)){
    		$str_order = sprintf(' ORDER BY %s %s', $sortname, $sortorder);
    	}
    
    	//组装SQL语句
    	if (!empty($type_search)){
    		$str_search = str_replace('WHERE AND', 'WHERE', $str_search);
    		$sql_count = $sql_count.$str_search;
    		$sql_search = $sql_search.$str_search.$str_order.$str_limit;
    	}else {
    		$sql_search = $sql_search.$str_order.$str_limit;
    	}
    
    	$value_count = empty($type_search) ? $this->dbca->stmt_query($sql_count) : $this->dbca->stmt_query($sql_count,$type_search,$param_search);
    
    	if ($value_count){
    
    		$return_data['code'] = 200;
    
    		//总记录条数
    		$return_data['message']['total'] = $value_count[0]->countNum;
    
    		//得到查询结果
    		$return_data['message']['data'] = empty($type_search) ? $this->dbca->stmt_query($sql_search) : $this->dbca->stmt_query($sql_search,$type_search,$param_search);
    
    	}else {
    		$return_data['code'] = 200;
    		$return_data['message']['total'] = 0;
    		$return_data['message']['data'] = '';
    	}
    
    	return $return_data;
    
    }
    
    /*
     * @abstract       delVote()     删除投票
    * @param          $importData   需要删除的投票的uuid组成的数组
    * @return         array('code','message');
    *  */
    public function delVote($importData){
    	//返回数组
    	$return_data = array();
    		
    	//错误信息
    	$error = '';
    
    	/* 传入数据有效性验证start */
    	if (!isset($importData) || empty($importData)){
    		$error .= '传值错误或未传值<br/>';
    		return array('code'=>400 , 'message'=>$error);
    	}
    
    	foreach ($importData as $uuid){
    		$content_vote = new entity('content_vote');
    		$content_vote->uuid = $uuid;
    		if (!$this->dbca->fetch($content_vote)){
    			return array('code'=>400 , 'message'=>'您的页面已过期，请刷新页面');
    		}
    	}
    
    	//删除投票
    	$sql_delVote = 'DELETE FROM content_vote WHERE uuid = ?';
    	//删除投票选项
    	$sql_delOption = 'DELETE FROM content_vote_option WHERE vote_uuid = ?';
    
    	try {
    			
    		$this->dbca->beginTransaction();
    			
    		foreach ($importData as $uuidItem){
    			$this->dbca->stmt_query($sql_delVote,'s',$uuidItem);
    			$this->dbca->stmt_query($sql_delOption,'s',$uuidItem);
    		}
    			
    		$this->dbca->commit();
    		return array('code'=>200,'message'=>'投票删除成功');
    			
    	}catch (Exception $e){
    		$this->dbca->rollBack();
    		$msg = $e->getMessage();
    		return array('code'=>500,'message'=>$msg);
    	}
    
    }
/****************************************************投票管理end********************************************************/


/****************************************************商品评论start*******************************************************/
   /*
    * @abstract       searchAllComment()    获得全部评论信息
    * @param          $importData
    * @return         $return_data('code','message'=>('data','total'));
    *  */
    public function searchAllComment($importData){
    	$sortname = isset($importData['sortname']) ? $importData['sortname'] : null;
    	$sortorder = isset($importData['sortorder']) ? $importData['sortorder'] : null;
    	$rp = isset($importData['rp']) ? $importData['rp'] : null;
    	$page = isset($importData['page']) ? $importData['page'] : null;
    	$uuid = isset($importData['uuid']) ? $importData['uuid'] : null;
    	if (empty($importData['uuid'])){
    		$sql = "SELECT * FROM content_comments ORDER BY '".$sortname."','".$sortorder."'";
    	}else {
    		$sql = "SELECT * FROM content_comments WHERE uuid = '".$uuid."'";
    	}
    	
    	return $result = $this->dbca->stmt_query($sql);
    	
    }
    
    /* 
     * @abstract      editComment()    回复评论
     * @param         $importData = array(必传：'operFlg','answerUserID'
     * 									     可选：'displayFlg'(单条必传),'recommendFlg'(单条必传),'answerInfo'(单条必传),'uuidArr'(批量必传),'uuid'(单条必传))
     * @return        array('code','message')
     *  */
    public function editComment($importData){
    	//错误信息
    	$error = '';
    	
    	$defaultMsg = '感谢您对本公司官网的支持！祝您购物愉快！';//批量回复时默认回复
    	$defaultDisFlg = 1;//批量回复时默认全部用户可见
    	$defaultRecFlg = 0;//批量回复时默认首页不可见
    	/* 传入数据有效性验证start */
    	//没有传入数据
    	if (empty($importData)) $error .= '没有传入任何数据<br/>';
    	
    	if (!isset($importData['operFlg'])){
    		$error .= '未传入operFlg<br/>';
    	}
    	
    	//operFlg：2，单条；3，批量
    	if ($importData['operFlg'] != 2 && $importData['operFlg'] != 3) {
    		$error .= '传入operFlg错误<br/>';
    	}
    	
    	if ($importData['operFlg'] == 2 && (!isset($importData['answerInfo']) || empty($importData['answerInfo']))) {
    		$error .= '回复不能为空<br/>';
    	}
    	
    	if (empty($importData['uuidArr']) && empty($importData['uuid'])) {
    		$error .= 'uuid传值错误<br/>';
    	}
    	    	
    	if ($importData['operFlg'] == 2 && (!isset($importData['displayFlg']) || !is_numeric($importData['displayFlg']))) {
    		$error .= '显示控制不能为空<br/>';
    	}
    	
    	if ($importData['operFlg'] == 2 && (!isset($importData['recommendFlg']) || !is_numeric($importData['recommendFlg']))) {
    		$error .= '是否首页显示不能为空<br/>';
    	}
    	
    	if (!isset($importData['answerUserID']) || empty($importData['answerUserID'])) {
    		$error .= '回复人不能为空<br/>';
    	}
    	
    	if (!empty($error)){
    		return array('code' => 400, 'message' => $error);
    	}
    	/* 传入数据有效性验证end */
    	
    	$answerInfo = empty($importData['answerInfo']) ? $defaultMsg : $importData['answerInfo'];//回复内容
    	$uuidArr = empty($importData['uuid']) ? $importData['uuidArr'] : array($importData['uuid']);//uuid
    	$displayFlg = empty($importData['displayFlg']) ? $defaultDisFlg : $importData['displayFlg'];//显示控制
    	$recommendFlg = empty($importData['recommendFlg']) ? $defaultRecFlg : trim($importData['recommendFlg']);//是否首页显示 
    	$answerUserID = trim($importData['answerUserID']);//回复人
		
    	//保存到数据库 
    	try {
	    	if (!empty($uuidArr)){
	    		foreach ($uuidArr as $uuid){
	    			$comment_obj = new entity('content_comments');
	    			$comment_obj->uuid = $uuid;
	    			
	    			if (!$this->dbca->fetch($comment_obj)){
	    				return $return_data = array( 'code' => 400, 'message' => '您的页面已过期，请刷新页面' );
	    			}
    				$comment_obj->commentStatus = 1;
    				$comment_obj->answerDateTime = date('Y-m-d H:i:s');
    				$comment_obj->answerInfo = htmlspecialchars($answerInfo);
    				$comment_obj->displayFlg = $displayFlg;
    				$comment_obj->recommendFlg = $recommendFlg;
    				$comment_obj->answerUserID = $answerUserID;
    				
    				$this->dbca->save($comment_obj,'content');
    				
	    		}
	    		return $return_data = array( 'code' => 200, 'message' => '回复成功' );
	    	}
	    	
    	}catch (Exception $e) {
    		error_log($e->getMessage());
    		return $return_data = array( 'code' => 500, 'message' => '系统错误' );
    	}
    	
    }
    
    public function deleteComment($importData){
    	//错误信息
    	$error = '';
    	
    	/* 传入数据有效性验证start */
    	//没有传入数据
    	if (empty($importData)) $error .= '没有传入任何数据<br/>';
    	     	 
    	if (empty($importData['uuidArr']) || !isset($importData['uuidArr'])) {
    		$error .= 'uuid传值错误<br/>';
    	}
    	    	    	 
    	if (!empty($error)){
    		return array('code' => 400, 'message' => $error);
    	}
    	/* 传入数据有效性验证end */
    	
    	$uuidArr = $importData['uuidArr'];//uuid数组
    	
    	//操作数据库
    	try {
	    	if (!empty($uuidArr)){
	    		//循环数组并根据uuid删除评论记录
	    		foreach ($uuidArr as $uuid){
	    			$comment_obj = new entity('content_comments');
	    			$comment_obj->uuid = $uuid;
	    			
	    			if (!$this->dbca->fetch($comment_obj)){
	    				return $return_data = array( 'code' => 400, 'message' => '您的页面已过期，请刷新页面' );
	    			}
	    			$this->dbca->delete($comment_obj);
	    		}
	    		return $return_data = array( 'code' => 200, 'message' => '删除成功' );
	    	}
    	}catch (Exception $e){
    		error_log($e->getMessage());
    		return $return_data = array( 'code' => 500, 'message' => '系统错误' );
    	}
    }


    /**
     * 查询文章分类
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
            'category_id' => int 文章分类ID
            'category_index' => int 文章分类索引
            'category_title' => int 文章分类标题
            'useFlg' => int 使用标志
            'sortname' => string 排序字段
            'sortorder' => string 排序方式
            'page' => int 分页页码
            'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchArticleCategory( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //文章分类ID
        if (isset($arg_array['category_id']) && is_numeric($arg_array['category_id'])) {
            $sql .= ' AND category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_id'];
        }

        //文章分类索引
        if (isset($arg_array['category_index']) && !empty($arg_array['category_index'])) {
            $sql .= ' AND category_index = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_index'];
        }

        //文章分类标题
        if (isset($arg_array['category_title']) && !empty($arg_array['category_title'])) {
            $sql .= ' AND category_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['category_title'] . '%';
        }

        //文章分类类型
        if (isset($arg_array['category_type']) && !empty($arg_array['category_type'])) {
            $sql .= ' AND category_type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['category_type'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_article_category ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_article_category ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'category_id' => $item->category_id,
                    'category_index' => $item->category_index,
                    'category_title' => $item->category_title,
                    'description' => $item->description,
                    'useFlg' => $item->useFlg,
                    'category_type' => $item->category_type
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    
    /**
     * 添加或修改文章分类信息
     * @param array $arg_array 文章分类信息
     * @param array $arg_array['id']    分类ID
     * @param array $arg_array['index']    分类别名
     * @param array $arg_array['title']    分类标题
     * @param array $arg_array['useFlg']    使用标志
     * @param array $arg_array['description']    备注
     * @param array $arg_array['creator']    操作人ID
     * @return array
     */
    public function addArticleCategory( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['id'], $arg_array['index'], $arg_array['title'], $arg_array['useFlg'], $arg_array['type'], $arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );
        //检查分类ID
        if (!is_numeric($arg_array['id']))
            return array( 'code' => 400, 'message' => '参数错误' );
        //检查分类别名
        if (empty($arg_array['index']))
            return array( 'code' => 400, 'message' => '分类别名不能为空' );
        //检查分类标题
        if (empty($arg_array['title']))
            return array( 'code' => 400, 'message' => '分类标题不能为空' );
        //检查分类
        if (!is_numeric($arg_array['type']) || $arg_array['type'] <= 0)
            return array( 'code' => 400, 'message' => '请选择分类类型' );
        //检查使用标志
        if (!in_array($arg_array['useFlg'], array(0, 1)))
            return array( 'code' => 400, 'message' => '请选择是否使用标志' );

        $commHandler = new commHandler();
        $arg_array['description'] = $commHandler->convertVar($arg_array['description']);

        try {

            $article_obj = new entity('content_article_category');
            if ($arg_array['id'] > 0) {
                $article_obj->category_id = $arg_array['id'];
                if (!$this->dbca->fetch($article_obj))
                    return array( 'code' => 404, 'message' => '文章分类信息没有找到' );
                $article_obj->editor = $arg_array['creator'];
            } else {
                $article_obj->creator = $arg_array['creator'];
            }
            $article_obj->category_index = $arg_array['index'];
            $article_obj->category_title = $arg_array['title'];
            $article_obj->category_type = $arg_array['type'];
            $article_obj->description = $arg_array['description'];
            $article_obj->useFlg = $arg_array['useFlg'];
            $this->dbca->save($article_obj, 'content');
			
            if ($arg_array['id'] == 0)
                return array( 'code' => 200, 'message' => '保存成功' );
            else
                return array( 'code' => 200, 'message' => '修改成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }


    }


    /**
     * 查询文章
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
            'article_category_id' => int 分类ID
            'article_id' => int 文章ID
            'article_title' => string 文章标题
            'useFlg' => int 使用标志
            'sortname' => string 排序字段
            'sortorder' => string 排序方式
            'page' => int 分页页码
            'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchArticle( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();


        //分类ID
        if (isset($arg_array['article_category_id']) && is_numeric($arg_array['article_category_id'])) {
            $sql .= ' AND article_category_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_category_id'];
        }

        //文章ID
        if (isset($arg_array['article_id']) && is_numeric($arg_array['article_id'])) {
            $sql .= ' AND article_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_id'];
        }

        //文章标题
        if (isset($arg_array['article_title']) && !empty($arg_array['article_title'])) {
            $sql .= ' AND article_title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['article_title'] . '%';
        }

        //文章分类索引
        if (isset($arg_array['article_category_index']) && !empty($arg_array['article_category_index'])) {
            $sql .= ' AND article_category_index = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['article_category_index'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //文章类型
        if (isset($arg_array['article_type']) && is_numeric($arg_array['article_type'])) {
            $sql .= ' AND article_type = ?';
            $params_type .= 'i'; 
            $params_array[] = $arg_array['article_type'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_article ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_article ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'article_id' => $item->article_id,
                    'article_title' => $item->article_title,
                    'useFlg' => $item->useFlg,
                    'commentFlg' => $item->commentFlg,
                    'article_pic' => $item->article_pic,
                    'article_link' => $item->article_link,
                    'article_summary' => $item->article_summary,
                    'description' => isset($arg_array['article_id']) ? $item->description : null,
                    'article_category_index' => $item->article_category_index,
                    'article_category_id' => $item->article_category_id,
                    'article_category_name' => $item->article_category_name,
                    'article_type' => $item->article_type,
                    'createTime' => $item->createTime,
                    'creator' => $item->creator,
                    'link' => $item->article_link ? $item->article_link : G_WEBSITE_DOMAINNAME . sprintf('/shop/article-%d.html', $item->article_id)
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存文章评论信息
     * @param array $arg_array 评论信息
     * @return array
     */
    public function addArticleComment( $arg_array ) {

        //检查是否缺少参数
        if (!isset($arg_array['pcustID'], $arg_array['creator'], $arg_array['info'], $arg_array['article_id']) || !is_numeric($arg_array['article_id']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        try {

            //查询文章信息
            $article_arg_array = array(
                'article_id' => $arg_array['article_id']
            );
            $article_array = $this->searchArticle( $article_arg_array );
            if (!$article_array) {
                return array( 'code' => 404, 'message' => '文章信息没有咋找到' );
            }

            //保存评论信息
            $article_comment = new entity('content_article_comments');
            $article_comment->creator = $arg_array['creator'];
            $article_comment->pcustID = $arg_array['pcustID'];
            $article_comment->commentInfo = $arg_array['info'];
            $article_comment->article_id = $article_array[0]['article_id'];
            $article_comment->article_title = $article_array[0]['article_title'];
            $article_comment->ipAddress = $arg_array['article_title'];
            $article_comment->commentStatus = 0;
            $this->dbca->save( $article_comment );

            return array( 'code' => 200, 'message' => '保存成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }


	/**
     * 查询商品咨询信息
     * @param array $arg_array                          查询参数数组
     * @param array $arg_array['startDate']    			起始时间
     * @param array $arg_array['endDate'] 				结束时间
     * @param array $arg_array['goodsName'] 			商品名称
     * @param array $arg_array['consultStatus']    		咨询处理状态
     * @param array $arg_array['displayFlg']      		网站显示控制
     * @param array $arg_array['sortname']              排序字段
     * @param array $arg_array['sortorder']             排序方式
     * @param array $arg_array['page']                  分页页码
     * @param array $arg_array['rp']                    分页页数
     * @return null|array
     */
    public function getGoodsConsultList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
		
        //咨询开始时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
        	$sql .= ' AND createTime >= ?';
        	$params_type .= 's';
        	$params_array[] = $arg_array['startDate'].' 00:00:00';
        	
        }
        
        //咨询结束时间
        if (isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
        	$sql .= ' AND createTime <= ?';
        	$params_type .= 's';
        	$params_array[] = $arg_array['endDate'].' '.date('H:i:s');
        }
        
        //商品名称
        if (isset($arg_array['goodsName']) && !empty($arg_array['goodsName'])) {
        	$sql .= ' AND goodsName LIKE ?';
        	$params_type .= 's';
        	$params_array[] = "%".$arg_array['goodsName']."%";
        }
        
        //咨询处理状态
        if (isset($arg_array['consultStatus']) && in_array($arg_array['consultStatus'],array('0','1'))) {
        	$sql .= ' AND consultStatus = ?';
        	$params_type .= 'i';
        	$params_array[] = $arg_array['consultStatus'];
        }
        
        //网站显示控制
        if (isset($arg_array['displayFlg']) && in_array($arg_array['displayFlg'],array('0','1'))) {
        	$sql .= ' AND displayFlg = ?';
        	$params_type .= 'i';
        	$params_array[] = $arg_array['displayFlg'];
        }
        
        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }
		
        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
				
                //统计商品咨询数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_consult ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;
				
                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询商品咨询信息
            $search_sql = 'SELECT * FROM content_consult ' . $sql;;
            $item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    
    /**
     * 删除商品咨询
     * @author raohenglei
     *
     */
    public function deleteGoodsConsult ( &$arg_array ) {

    	if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的商品咨询' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$consult_uuid) {

                if (strlen($consult_uuid) != 32)
                    return array( 'code' => 400, 'message' => '参数错误' );

                //查询资源位信息
                $consult_obj = new entity('content_consult');
                $consult_obj->uuid = $consult_uuid;
                if ($this->dbca->fetch($consult_obj)) {
                    $this->dbca->delete($consult_obj);
                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }
    }


    /**
     * 商品咨询处理
     * @author raohenglei
     * @param array $arg_array 参数数组
     * @return array
     */
    public function dealGoodsConsult ( $arg_array , $mgr = null) {

        $uuid = $arg_array['uuid'];
    	
    	try{

			$getConsultInfo 				= new entity('content_consult');
			$getConsultInfo->uuid 			= $uuid;
			
			if (!$this->dbca->fetch($getConsultInfo)) {
				throw new Exception('商品咨询信息没有找到，请与管理员联系', 404);
			}
			$getConsultInfo->answerInfo		= $arg_array['content'];
			$getConsultInfo->consultStatus 	= 1;
			$getConsultInfo->answerUserID 	= $arg_array['answerUserID'];
			$getConsultInfo->answerDateTime = $arg_array['answerDateTime'];
			$getConsultInfo->displayFlg 	= $arg_array['displayFlg'];
			$getConsultInfo->recommendFlg 	= $arg_array['recommendFlg'];
			$this->dbca->save($getConsultInfo,'content');
			
			return array( 'code' => 200, 'message' => '商品咨询已处理' );
			
		} catch (Exception $e) {

			if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
				return array( 'code' => 500, 'message' => '系统异常，请与管理员联系' );
			}

		}
    }

    /**
     * 保存商品咨询信息
     * @param $arg_array
     * $arg_array = array(
            'info' => string 咨询内容
            'postID' => int 三级商品网购号
            'pcustID' => int 客户号
            'ip' => string ip地址
     * )
     * @return array
     */
    public function addGoodsConsult( $arg_array ) {

        //检查参数
        if (!isset($arg_array['info'], $arg_array['postID'], $arg_array['pcustID'], $arg_array['ip']) || !is_numeric($arg_array['twoPostID']) || !is_numeric($arg_array['pcustID']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['info']))
            return array( 'code' => 400, 'message' => '咨询内容不能为空' );

        //实例公共类
        $commHandler = new commHandler( $this->dbca );

        try {

			//网购号
			$postID = $arg_array['postID'];
			$info   = $arg_array['info'];
			$ip     = $arg_array['ip'];

            //检查会员信息
            $arg_array = array( 'pcustID' => $arg_array['pcustID'] );
            $cust_array = $commHandler->search($arg_array, 'cust_master');
            if (!$cust_array)
                return array( 'code' => 404, 'message' => '会员信息没有找到' );
            $cust_obj = &$cust_array[0];

            //查询商品信息
            $arg_array = array( 'postID' => $postID );
            $goods_array = $commHandler->search($arg_array, 'goods_sale');
            
            if (!$goods_array)
                return array( 'code' => 404, 'message' => '商品信息没有找到' );
            $goods_obj = &$goods_array[0];

            //保存咨询信息
            $consult_obj = new entity('content_consult');
            $consult_obj->creator = $cust_obj->custID;
            $consult_obj->pcustID = $cust_obj->pcustID;
            $consult_obj->consultInfo = $commHandler->convertVar($info);
            $consult_obj->postID = $postID;
            $consult_obj->twoPostID = $goods_obj->twoPostID;
            $consult_obj->goodsName = $goods_obj->goodsName;
            $consult_obj->goodsMainPic = $goods_obj->goodsMainPic;
            $consult_obj->ipAddress = $ip;
            $consult_obj->consultStatus = 0;
            $this->dbca->save($consult_obj);

            return array( 'code' => 200, 'message' => '商品咨询保存成功' );

        } catch (Exception $e) {

            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }

    /**
     * 查询文章评价信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'pcustID' => int 客户号
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchArticleComments( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //客户号
        if (isset($arg_array['pcustID']) && is_numeric($arg_array['pcustID'])) {
            $sql .= ' AND cac.pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //UUID
        if (isset($arg_array['uuid']) && is_string($arg_array['uuid'])) {
            $sql .= ' AND cac.uuid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['uuid'];
        }

        //文章ID
        if (isset($arg_array['article_id']) &&  is_numeric($arg_array['article_id']) ) {
            $sql .= ' AND cac.article_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['article_id'];
        }

        //回复状态 0:不推荐 1:推荐
        if (isset($arg_array['commentStatus']) && in_array($arg_array['commentStatus'], array(0 ,1))) {
            $sql .= ' AND cac.recommendFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['recommendFlg'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_article_comments cac
                               INNER JOIN cust_master cm ON cm.pcustID = cac.pcustID ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT cac.*,cm.custID,cm.head_pic,cm.rankName FROM content_article_comments cac
                           INNER JOIN cust_master cm ON cm.pcustID = cac.pcustID ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'pcustID' => $item->pcustID,
                    'custID' => $item->custID,
                    'head_pic' => set_cust_head_pic( $item->head_pic ),
                    'rankName' => $item->rankName,
                    'commentInfo' => $item->commentInfo,
                    'article_id' =>$item->article_id,
                    'article_title' => $item->article_title,
                    'commentStatus' => $item->commentStatus,
                    'answerInfo' => $item->answerInfo,
                    'answerUserID' => $item->answerUserID,
                    'answerDateTime' => $item->answerDateTime
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 保存文章回复内容
     */
    public function  articleReply( $arg_array ){

        if( !isset( $arg_array['operator'] , $arg_array['answerInfo'] ) )
            return array( 'code'=>400 , 'message'=>'参数错误' );

        if( empty($arg_array['operator']) )
            return array( 'code'=>400 , 'message'=>'回复人不能为空' );

        if( empty($arg_array['answerInfo']) )
            return array( 'code'=>400 , 'message'=>'回复内容不能为空' );

        $uuid = isset($arg_array['uuid'])?$arg_array['uuid']: NULL;

        try {

            $article_comment_obj = new entity('content_article_comments');

            if( $uuid ){

                $article_comment_obj->uuid = $uuid;

                if( !$this->dbca->fetch($article_comment_obj) )
                    throw new Exception( '没有找到文章评论信息' , 404 );

            }

            $article_comment_obj->commentStatus = 1;
            $article_comment_obj->answerDateTime = date('Y-m-d H:i:s');
            $article_comment_obj->answerInfo = htmlspecialchars($arg_array['answerInfo']);
            $article_comment_obj->answerUserID = $arg_array['operator'];

            $this->dbca->save($article_comment_obj);

            return $return_data = array( 'code' => 200, 'message' => '回复成功' );

        }catch (Exception $e) {

            if( in_array( $e->getCode() , array(400,404,406) ) ){
                return $return_data = array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            }else{
                error_log($e->getMessage());
                return $return_data = array( 'code' => 500, 'message' => '系统错误,回复失败' );
            }

        }

    }

    /**
     * 删除文章评论
     * $arg_array = array(uuid,uuid.uuid)  uuid组成的数组
     */
    public function delArticleComment( $arg_array ){

        if( !is_array($arg_array) || empty($arg_array) )
            return array( 'code'=>400 , 'message'=>'参数错误' );

        //统计不存在的信息的数目
        $errorNum = 0;

        //记录要删除的entity对象
        $deleteObjArr = array();

        //检查要删除的信息是否全部存在
        foreach( $arg_array as $uuid ){

            //检查信息是否存在
            $check_obj = new entity('content_article_comments');
            $check_obj->uuid = $uuid;

            if( !$this->dbca->fetch( $check_obj ) )
                $errorNum++;

            $deleteObjArr[] = $check_obj;

        }

        if( $errorNum > 0 )
           return array('message'=>'所选部分信息已不存在，请刷新页面' , 'code'=>400 );

        try{

            //开启事务
            $this->dbca->beginTransaction();

            //循环删除
            foreach( $deleteObjArr as $delobj ){

                $this->dbca->delete( $delobj );

            }

            //提交事务
            $this->dbca->commit();

            return array('message'=>'删除成功', 'code'=>200);

        }catch( Exception $e ){

            //回滚事务
            $this->dbca->rollBack();

            error_log($e->getMessage());
            return array('message'=>'系统错误，操作失败', 'code'=>500);

        }

    }

    /**
     * 客户建议查询
     * @param $arg_array
     * $arg_array = array (
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return array|null
     */
    public function searchCustSuggest(&$arg_array){

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //建议ID
        if (isset($arg_array['suggestID']) && is_numeric($arg_array['suggestID'])) {
            $sql .= ' AND suggestID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['suggestID'];
        }

        //建议人姓名
        if (isset($arg_array['custName']) && !empty($arg_array['custName'])) {
            $sql .= ' AND custName like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['custName'].'%';
        }

        //建议状态
        if (isset($arg_array['suggestStatus']) && in_array($arg_array['suggestStatus'],array(0,1,2))) {
            $sql .= ' AND suggestStatus = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['suggestStatus']);
        }

        //创建时间
        if (isset($arg_array['startDate']) && !empty($arg_array['startDate'])) {
            $sql .= ' AND createTime >= ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['startDate']);
        }

        if (isset($arg_array['endDate']) && !empty($arg_array['endDate'])) {
            $sql .= ' AND createTime <= ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['endDate']);
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_suggest ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询备注信息
            $search_sql = 'SELECT * FROM content_suggest ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            if ($temp_item_array) {
                foreach ($temp_item_array as &$item) {
                    $item_array[] = array(
                        'createTime'=> $item->createTime,
                        'pcustID' => $item->pcustID,
                        'custID' => $item->custID,
                        'suggestID' => $item->suggestID,
                        'content' => $item->content,
                        'custName' => $item->custName,
                        'mobile' => $item->mobile,
                        'suggestStatus' => $item->suggestStatus,
                        'reply' => $item->reply,
                        'auditor' => $item->auditor,
                        'auditTime' => $item->auditTime
                    );
                }
            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 客户建议删除
     * $arg_array = array(
     *      delete_str => 客户建议ID
     * )
     * @param $arg_array
     * @return array|null
     */
    public function suggestDelete(&$arg_array){

        if(!isset($arg_array['delete_str']) || empty($arg_array['delete_str']))
            return array('code'=>400,'message'=>'参数错误');

        $delete_arr = explode('_',$arg_array['delete_str']);

        try{
            foreach($delete_arr as $val){
                $this->dbca->stmt_query('DELETE FROM content_suggest WHERE suggestID = ?','i',$val);
            }

            return array('code'=>200,'message'=>'删除成功');
        }catch (Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 处理客户建议
     * @param $arg_array
     *  $arg_array = array (
     *     suggestID => 建议ID
     * )
     * @return array|null
     */
    public function suggestHandle(&$arg_array){
        if(!isset($arg_array['suggestID']) || empty($arg_array['suggestID']))
            return array('code'=>400,'message'=>'参数错误');

        $arr['suggestID'] = $arg_array['suggestID'];

        try{
            $suggest_data = $this->searchCustSuggest($arr);

            return array('code'=>200,'suggest_data'=>$suggest_data);

        }catch (Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 提交处理客户建议
     * @param $arg_array
     *  $arg_array = array (
     *     [dt] => array(
                        [pcustID] => 会员号
                        [custID] => 会员名
                        [suggestID] => 建议ID
                        [suggestStatus] => 建议状态
                        [reply] =>回复内容
                        )
     *      [eai] => EAI
     *      [auditor] => 处理人
     * )
     * @return array|null
     */
    public function suggestHandleSubmit(&$arg_array){

        if(!isset($arg_array['dt']['suggestID']) || empty($arg_array['dt']['suggestID']))
            return array('code'=>400,'message'=>'参数错误');

        try{

            //判断审核还是作废
            if($arg_array['dt']['suggestStatus'] == 1){

                //检查用户是否存在
                $custInfo = $this->dbca->stmt_query('SELECT * FROM cust_master where pcustID = ? ','i',$arg_array['dt']['pcustID']);

                if(!$custInfo || $custInfo[0]->useFlg = 0)
                    return array('code'=>400,'message'=>'会员不存在或禁用');

                quanli_include('module/base/promotionHandler.class.php');    //加载促销活动类

                $promotionHandler = new promotionHandler( $this->dbca );  //实例化促销类

                $promotionHandler->grantCustPromotion( $arg_array['dt']['pcustID'], $arg_array['dt']['custID'], 4, $arg_array['eai'] ); //查询会员促销活动并发放
            }

            $content_suggest = new entity('content_suggest');
            $content_suggest->suggestID = $arg_array['dt']['suggestID'];
            if($this->dbca->fetch($content_suggest)){
                $content_suggest->suggestStatus = $arg_array['dt']['suggestStatus'];
                $content_suggest->reply = $arg_array['dt']['reply'];
                $content_suggest->auditTime = date('y-m-d h:i:s');
                $content_suggest->auditor = $arg_array['auditor'];
                $this->dbca->save($content_suggest);
                return array('code'=>200,'message'=>'客户建议处理成功');
            }else{
                return array('code'=>400,'message'=>'该客户建议不存在');
            }

        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }
    }
	
    /**
     * 导航列表查询
     *
     * @param array $arg_array
     * @return unknown
     */
	public function searchNavList($arg_array){ 

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_navigation ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询备注信息
            $search_sql = 'SELECT * FROM content_navigation ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            if ($temp_item_array) {
                foreach ($temp_item_array as &$item) {
                    $item_array[] = array(
                        'createTime'=> $item->createTime,
                        'navID' => $item->navID,
                        'navName' => $item->navName,
                        'navLink' => $item->navLink,
                        'navSort' => $item->navSort
                    );
                }
            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
	
	//保存导航栏设置、
    public function saveNavigation($arg_array){

        if(!isset($arg_array['data']) || empty($arg_array['data']))
            return array('code'=>400,'message'=>'参数错误');

        try{

            //删除导航栏

            $this->dbca->stmt_query("DELETE FROM  content_navigation");

            foreach($arg_array['data'] as $val){

                $content_navigation = new entity('content_navigation');
                $content_navigation->creator = $arg_array['creator'];
                $content_navigation->navName = $val[0];
                $content_navigation->navLink = $val[1];
                $content_navigation->navSort = $val[2];
                $this->dbca->save($content_navigation);

            }

            return array('code'=>200,'message'=>'保存成功');

        }catch(Exception $e){

            error_log($e->getMessage());
            return null;

        }

    }
    
    /**
     * 查询内容中心信息
     * @param array $arg_array                          查询参数数组
     * $arg_array = array (
     *       'pcustID' => int 客户号
     *       'sortname' => string 排序字段
     *       'sortorder' => string 排序方式
     *       'page' => int 分页页码
     *       'rp' => int 分页页数
     * )
     * @return null
     */
    public function searchContentCenterList( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //内容中心ID
        if (isset($arg_array['contentID']) && is_numeric($arg_array['contentID'])) {
            $sql .= ' AND contentID = ?';
            $params_type .= 'i';
            $params_array[] = trim($arg_array['contentID']);
        }

        //内容中心标题
        if (isset($arg_array['contentTitle']) && !empty($arg_array['contentTitle'])) {
            $sql .= ' AND contentTitle LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['contentTitle']) . '%';
        }
        
        //关键字
        if (isset($arg_array['searchKeys']) && !empty($arg_array['searchKeys'])) {
            $sql .= ' AND searchKeys LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['searchKeys']) . '%';
        }
		
        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_center ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_center ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array) {
                return null;
            }    

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'createTime' => $item->createTime,
                    'contentID' => $item->contentID,
                    'contentTitle' => $item->contentTitle,
                    'useFlg' => $item->useFlg,
                    'contentSort' => $item->contentSort,
                    'contentSummary' => $item->contentSummary,
                    'searchKeys' => $item->searchKeys,
                    'description' => $item->description,
                    'isethead' => $item->isethead,
                    'isetstern' =>$item->isetstern,
                    'isetpage' => $item->isetpage,
                    'keyword' => $item->keyword,
                    'postIDStr' => $item->postIDStr
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }
    
    /**
     * 保存内容中心
     *
     * @param 参数数组 $arg_array
     */
    public function saveContentCenter($arg_array){
    	
    	//检查参数
    	if (!isset($arg_array['content']) || empty($arg_array['content'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	//内容标题
    	if (!isset($arg_array['content']['contentTitle']) || empty($arg_array['content']['contentTitle'])) {
    		return array('code' => 400, 'message' => '请填写内容标题');
    	}
    	
    	//排列序号
    	if (!isset($arg_array['content']['contentSort']) || empty($arg_array['content']['contentSort'])) {
    		return array('code' => 400, 'message' => '请填写排序序号');
    	}
    	
    	//排列序号是否重复
    	$check_content_center = new entity('content_center');
    	$check_content_center->contentSort = $arg_array['content']['contentSort'];
    	if ($this->dbca->fetch($check_content_center)) {
    		if (!empty($arg_array['content']['contentID'])) {
    			if ($check_content_center->contentID != $arg_array['content']['contentID']) {
    				return array('code' => 400, 'message' => sprintf('排列序号「%s」已经存在',$arg_array['content']['contentSort']));
    			}
    		} else {
    			return array('code' => 400, 'message' => sprintf('排列序号「%s」已经存在',$arg_array['content']['contentSort']));
    		}
    		
    	}
    	
    	//内容中心描述
    	if (!isset($arg_array['content']['description']) || empty($arg_array['content']['description'])) {
    		return array('code' => 400, 'message' => '请输入内容中心描述');
    	}
    	
    	$save_content_center = new entity('content_center');
    	if (isset($arg_array['content']['contentID']) && !empty($arg_array['content']['contentID'])) {
    		$save_content_center->contentID = $arg_array['content']['contentID'];
    		if (!$this->dbca->fetch($save_content_center)) {
    			return array('code' => 400, 'message' => '内容中心保存失败');
    		}
    		
    	} else {
    		$save_content_center->creator = $arg_array['creator'];
    	}
    	
    	$save_content_center->contentTitle 	 = $arg_array['content']['contentTitle'];
    	$save_content_center->useFlg 		 = 1;
    	$save_content_center->contentSort 	 = $arg_array['content']['contentSort'];
    	$save_content_center->contentSummary = $arg_array['content']['contentSummary'];
    	$save_content_center->searchKeys 	 = $arg_array['content']['searchKeys'];
    	$save_content_center->description 	 = $arg_array['content']['description'];
    	$save_content_center->isethead 	     = $arg_array['content']['isethead'];
    	$save_content_center->isetstern 	 = $arg_array['content']['isetstern'];
    	$save_content_center->isetpage 		 = $arg_array['content']['isetpage'];
    	$save_content_center->keyword 		 = $arg_array['content']['keyword'];
    	$save_content_center->postIDStr 	 = $arg_array['content']['postIDStr'];
    	
    	$this->dbca->save($save_content_center);
    	
    	if (isset($arg_array['content']['contentID']) && !empty($arg_array['content']['contentID'])) {
			return array('code' => 200, 'message' => '内容中心更新成功', 'type' => 2);
    	} else {
    		return array('code' => 200, 'message' => '内容中心保存成功', 'type' => 1);
    	}
    }

    /**
     * 保存订单商品评价
     * @param $arg_array
     * $arg_array = array(
     *       'bill_no' => string 订单号
     *       'goods' => array 评价信息
     * )
     * @return array
     */
    public function addGoodsComments( $arg_array ) {

        if (!isset($arg_array['bill_no'], $arg_array['goods']) || empty($arg_array['bill_no']) || !is_array($arg_array['goods']) || empty($arg_array['goods'])) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        //实例公共类
        $commHandler = new commHandler( $this->dbca );

        //实例订单类
        $billHandler = new billHandler( $this->dbca );

        //实例会员类
        $customerHandler = new customerHandler( $this->dbca );

        $goods_obj_array = array();

        $comment_obj_array = array();

        try {

            //查询会员信息
            $temp_arg_array = array(
                'pcustID' => $arg_array['pcustID']
            );
            $cust_array = $customerHandler->getCustomerInfo( $temp_arg_array );
            if (!$cust_array) {
                throw new Exception('会员信息没有找到', 404);
            }

            //查询订单信息
            $temp_arg_array = array(
                'bill_no' => $arg_array['bill_no'],
                'pcustID' => $arg_array['pcustID']
            );
            $bill_array = $billHandler->searchBillInfo( $temp_arg_array );
            if (!$bill_array) {
                throw new Exception('订单信息没有找到', 404);
            }
            if (!$bill_array || in_array($bill_array[0]['bill_status'], array(0, 1, 2, 3, 5))) {
                throw new Exception('订单未完成无法评价商品', 404);
            }

            //查询订单商品信息
            $temp_arg_array = array(
                'bill_no' => $arg_array['bill_no']
            );
            $bill_goods_array = $billHandler->searchBillGoods( $temp_arg_array );
            if (!$bill_goods_array) {
                throw new Exception('订单商品信息没有找到', 404);
            }

            //循环订单商品，保存商品评价信息
            foreach($bill_goods_array as &$bill_goods) {

                $id = $bill_goods['bill_goods_no'];
                if (!isset($arg_array['goods'][$id])) {
                    throw new Exception('请选择商品评价', 404);
                }

                $comment = &$arg_array['goods'][$id];
                if (!isset($comment['star']) || !in_array($comment['star'], array(1, 2, 3, 4, 5))) {
                    throw new Exception('请选择商品评价', 404);
                }

                if (!isset($comment['content']) || empty($comment['content'])) {
                    throw new Exception('请输入商品评论', 404);
                }

                //生成评价数据
                $comment_obj = new entity('content_comments');
                $comment_obj->creator = $cust_array[0]['custID'];
                $comment_obj->pcustID = $arg_array['pcustID'];
                $comment_obj->bill_no = $arg_array['bill_no'];
                $comment_obj->commentInfo = $commHandler->convertVar($comment['content']);
                $comment_obj->postID = $bill_goods['postID'];
                $comment_obj->twoPostID = $bill_goods['twoPostID'];
                $comment_obj->goodsName = $bill_goods['goodsName'];
                $comment_obj->goodsMainPic = $bill_goods['goodsMainPic'];
                $comment_obj->ipAddress = $arg_array['ip'];
                $comment_obj->commentStatus = 0;
                $comment_obj->commentLevel = $comment['star'];
                $comment_obj_array[] = $comment_obj;

                //更新订单商品
                $bill_goods_obj = new entity('bill_goods');
                $bill_goods_obj->bill_goods_no = $id;
                $this->dbca->fetch($bill_goods_obj);
                $bill_goods_obj->comment_flg = 1;
                $goods_obj_array[] = $bill_goods_obj;

            }

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

        try {

            $this->dbca->beginTransaction();

            foreach($comment_obj_array as &$comment_obj) {
                $this->dbca->save($comment_obj);
            }

            foreach($goods_obj_array as &$goods_obj) {
                $this->dbca->save($goods_obj);
            }

            $this->dbca->commit();

        } catch (Exception $e) {

            $this->dbca->rollBack();
            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

        return array( 'code' => 200, 'message' => '商品评价保存成功' );

    }

}
